package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.fc.entity.vo.FcTreeVo;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.ContractTemplateMapper;
import cn.lg.soar.boot.fc.entity.ContractTemplate;
import cn.lg.soar.boot.fc.service.ContractTemplateService;

import java.util.ArrayList;
import java.util.List;

/**
 * 合同模板表
 * @author ZhuanZ（无密码）
 * @since 2025-02-17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class ContractTemplateServiceImpl extends BaseServiceImpl<ContractTemplateMapper, ContractTemplate> implements ContractTemplateService {


    @Override
    public PageResult<FcTreeVo> queryResultVo(PageQuery pageQuery, QueryParams params) {
        PageResult<FcTreeVo> pageResultVo = new PageResult<>();

        SqueryWapper<ContractTemplate> squeryWapper = new SqueryWapper<>(ContractTemplate.class);
        PageResult<ContractTemplate> pageResult = squeryWapper.query(this, pageQuery, params);

        // 对象拷贝
        BeanUtils.copyProperties(pageQuery, pageResultVo);
        // 返回vo
        List<FcTreeVo> listVo = new ArrayList<>();

        pageResult.getList().forEach(item-> {

            FcTreeVo fcTreeVo = new FcTreeVo();

            BeanUtils.copyProperties(item, fcTreeVo);
            fcTreeVo.setParentId("0");
            fcTreeVo.setId(String.valueOf(item.getId()));
            listVo.add(fcTreeVo);
        });

        pageResultVo.setList(listVo);
        pageResultVo.setTotal(Long.valueOf(listVo.size()));

        return pageResultVo;
    }

    @Override
    public ContractTemplate getByName(String name) {
        if (StringUtils.isBlank(name)) {
            return new ContractTemplate();
        }
        return this.getFirst(Wrappers.<ContractTemplate>lambdaQuery().eq(ContractTemplate::getName, name));
    }
}
